import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.HashMap;
import java.util.SortedMap;
import javax.swing.DefaultListModel;


import javax.swing.JSeparator;

public class VueRechercheMotCle extends Vue {
	private final JSeparator separator = new JSeparator();
	private JButton bntAnnuler;
	private JButton btnRechercher;
	private JButton btnAnnuler2;
	DefaultListModel dlm = new DefaultListModel();
	DefaultListModel dlmOuvrage = new DefaultListModel();
	DefaultListModel dlmArticle = new DefaultListModel();

	
	private JList listMotcle;
	private JTextField textFieldMot;

	private JScrollPane scrollPaneMotcle;
	private JList listOuvrage;
	private JScrollPane scrollPaneOuvrage;
	private JList listArticle;
	private JScrollPane scrollPaneArticle;

	public VueRechercheMotCle(Controleur controleur) {
		super(controleur);
		setTitle("Recherche par mot cle");
		getContentPane().setLayout(null);
		setBounds(100, 100, 500, 550);

		scrollPaneMotcle = new JScrollPane();
		scrollPaneMotcle.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPaneMotcle.setBounds(132, 24, 257, 104);
		getContentPane().add(scrollPaneMotcle);
		
		listMotcle = new JList(dlm);
		scrollPaneMotcle.setViewportView(listMotcle);
		
		scrollPaneOuvrage = new JScrollPane();
		scrollPaneOuvrage.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPaneOuvrage.setBounds(132, 212, 257, 88);
		getContentPane().add(scrollPaneOuvrage);
		
		listOuvrage = new JList(dlmOuvrage);
		scrollPaneOuvrage.setViewportView(listOuvrage);
		
		scrollPaneArticle = new JScrollPane();
		scrollPaneArticle.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPaneArticle.setBounds(132, 334, 257, 88);
		getContentPane().add(scrollPaneArticle);
		
		listArticle = new JList(dlmArticle);
		scrollPaneArticle.setViewportView(listArticle);
		
		JLabel lblMotCles = new JLabel("Mot cles:");
		lblMotCles.setBounds(53, 26, 61, 15);
		getContentPane().add(lblMotCles);
		
		JLabel lblOuvrage = new JLabel("Ouvrages :");
		lblOuvrage.setBounds(41, 213, 73, 17);
		getContentPane().add(lblOuvrage);
		
		JLabel lblArticle = new JLabel("Articles :");
		lblArticle.setBounds(53, 336, 61, 15);
		getContentPane().add(lblArticle);
		
		btnAnnuler2 = new JButton("Fermer");
		btnAnnuler2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getControleur().fermerVue(VueRechercheMotCle.this);

			}
		});
		btnAnnuler2.setBounds(209, 459, 107, 25);
		getContentPane().add(btnAnnuler2);
		separator.setBounds(0, 169, 501, 33);
		getContentPane().add(separator);
		
		bntAnnuler = new JButton("Annuler");
		bntAnnuler.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getControleur().fermerVue(VueRechercheMotCle.this);
			}
		});
		bntAnnuler.setBounds(132, 140, 107, 25);
		getContentPane().add(bntAnnuler);
				
		btnRechercher = new JButton("Rechercher");
		btnRechercher.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dlmOuvrage.clear();	
				dlmArticle.clear();
				//recuperation du mot cle
				String s = listMotcle.getSelectedValue().toString();
				if (!s.isEmpty())
					getControleur().rechOuvrageMotcle(s);
			}
		});
		btnRechercher.setBounds(282, 140, 107, 25);
		getContentPane().add(btnRechercher);
		// TODO Auto-generated constructor stub
	}
	public void setEtat (int etat){
		switch (etat) {
		case initiale: {
			btnAnnuler2.setEnabled(true);
			break;
			}
		}
	}
	public void alimenteMotcle(SortedMap<String, Motcle> dico) {
		dlm.clear();
		for (Motcle motcle : dico.values()) {
			dlm.addElement(motcle.getMotCle());
		}
	}
	public void alimenteOuvrage (HashMap<String, Ouvrage> dico) {
		dlmOuvrage.clear();
		for (Ouvrage ouvrage : dico.values()) {
			dlmOuvrage.addElement(ouvrage.getTitre());
		}
	}
	public void alimenteArticle (HashMap<String, Article> dico) {
		dlmArticle.clear();
		for (Article article : dico.values()) {
			String str =article.getTitre()  +"( id : "+ article.getParution().getId() +") : "+ article.getParution().getPeriodique().getTitre();
			dlmArticle.addElement(str);
		}
	}	
}
